<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery1.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="group" v-if="groupCall">事业部</el-menu-item>
      <el-menu-item index="department" v-if="departmentCall">部门</el-menu-item>
      <el-menu-item index="all" v-if="allCall">全部</el-menu-item>
    </el-menu>

    <div style="margin-top:40px">
      <el-form :inline="true" :model="listQuery1" size="mini" label-width="100px">
        <el-form-item label="日期：">
          <el-date-picker
            v-model="listQuery1.time"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

<!--        <el-form-item label="新老客户类型：">-->
<!--          <el-select v-model="listQuery1.customerType" filterable placeholder="请选择" clearable>-->
<!--            <el-option :key="1" label="新客户" value="new"> </el-option>-->
<!--            <el-option :key="2" label="老客户" value="old"> </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="业务员：" v-if="listQuery1.tab !='my'">
          <el-select v-model="listQuery1.userId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门：" v-if="listQuery1.tab =='all'">
          <el-cascader
            v-model="listQuery1.departmentId"
            :options="data"
            :props="{value:'departmentId',label:'departmentName',emitPath:false}"
            clearable>
          </el-cascader>
        </el-form-item>

        <el-form-item>
          <el-button
            @click="handleSearchList1()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading1"
        :data="tableData1"
        max-height="460"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="业务员" min-width="100" prop="orderSn" align="center">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column
          label="归属军团"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.departmentName2}}
          </template>
        </el-table-column>
        <el-table-column
          label="归属部门"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.departmentName}}
          </template>
        </el-table-column>
        <el-table-column
          label="外呼总数"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.all}}
          </template>
        </el-table-column>
        <el-table-column
          label="拨通数"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.callAll}}
          </template>
        </el-table-column>
        <el-table-column
          label="接通率"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.callRatio}}
          </template>
        </el-table-column>
        <el-table-column
          label="通话总时长"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.callTimeAll}}
          </template>
        </el-table-column>
        <el-table-column
          label="平均通话时长"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.averageRatio}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="最长通话时长"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.maxTime}}
          </template>
        </el-table-column>
        <el-table-column
          label="最短通话时长"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.minTime}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
<!--      <el-pagination-->
<!--        background-->
<!--        @size-change="handleSizeChange1"-->
<!--        @current-change="handleCurrentChange1"-->
<!--        layout="total, sizes,prev, pager, next,jumper"-->
<!--        :current-page.sync="listQuery1.pageNum"-->
<!--        :page-size="listQuery1.pageSize"-->
<!--        :page-sizes="[10,15,20]"-->
<!--        :total="count1">-->
<!--      </el-pagination>-->
    </div>

  </div>
</template>
<script>
import axios from 'axios'
import {getOrderUsersList,} from '@/api/partner'
import {tongXingLogForms} from '@/api/fms.js'
import store from '@/store'
import { mapGetters } from 'vuex'
import {getDeptList} from '@/api/management'
import { formatDate } from '@/utils/date.js';

const defaultListQuery={
  "time" : [],//时间
  "userId" : null,//业务员
  tab:'my',//my 我的，department 部门，all 全部
  departmentId:'',//部门
  customerType:'',//new 新客户 old 老客户
};



export default {
  name: "index",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery1: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData1: [],
      count1: 0,
      listLoading1: false,
      userOptionList:[],
      data:[]

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    departmentCall (){
      if(this.roles.indexOf("departmentCall") != -1){
        return true
      }else{
        return false
      }
    },
    allCall (){
      if(this.roles.indexOf("allCall") != -1){
        return true
      }else{
        return false
      }
    },
    groupCall (){
      if(this.roles.indexOf("groupCall") != -1){
        return true
      }else{
        return false
      }
    },

  },
  created() {
    this.getUserOptionList();
    this.getBuMenList();
    let time = formatDate(new Date(), 'yyyy-MM-dd')
    this.listQuery1.time = [time,time]
    this.getList1();
  },
  filters: {


  },
  methods:{
    handleSelect(key){
      this.listQuery1 = JSON.parse(JSON.stringify(defaultListQuery))
      let time = formatDate(new Date(), 'yyyy-MM-dd')
      this.listQuery1.time = [time,time]
      this.listQuery1.tab = key
      this.handleSearchList1()
    },
   //去除不要的children
    forTree(list){
      let newList = list;
      if(newList.length){
        for (let i = 0; i<newList.length; i++){
          if(newList[i].children && newList[i].children.length > 0){
            this.forTree(newList[i].children)
          }else{
            delete newList[i].children;
          }
        }
        return newList
      }
    },

    //获取部门列表
    getBuMenList(){
      getDeptList().then((res)=>{
        this.data = this.forTree(res);
      })
    },
    //获取列表
    getList1(){
      this.listLoading1 = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery1));
      tongXingLogForms(listQuery).then(response => {
        this.tableData1 = response.list;
        this.count1 = response.count;
        this.listLoading1 = false;
      }).catch(()=>{
        this.listLoading1 = false;
      });
    },

    handleSizeChange1(val){
      this.listQuery1.pageNum = 1;
      this.listQuery1.pageSize = val;
      this.getList1();
    },
    handleCurrentChange1(val){
      this.listQuery1.pageNum = val;
      this.getList1();
    },

    handleSearchList1(){
      this.listQuery1.pageNum = 1;
      this.getList1();
    },

    //获取员工下拉选项
    getUserOptionList(){
      getOrderUsersList({type:'sale'}).then((res)=>{
        this.userOptionList = res;
      })
    },



  },
}
</script>

<style scoped lang="scss">
.upload-div{
  text-align: center;
  position:relative;
  #upload{

    opacity: 0;
    position: absolute;
    z-index: 1;
    width:154px;
    height:40px
  }
}
.title-tab{
  border-radius: 8px;
  background: #eee;
  margin-left: 10px;
  display: inline-block;
  vertical-align: middle;
  line-height: 20px;
  padding: 0 5px;
  color: #666;
}
</style>
